<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\controller\News;
use think\db;
/**
 * 是否提供服务
 *
 * @icon fa fa-circle-o
 */
class Twotg extends Backend
{
    
    /**
     * Twotg模型对象
     * @var \app\admin\model\Twotg
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Twotg;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("qyList", $this->model->getqyList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['byduser'])
                ->where($where)
                ->where('state','<>',2)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {


            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if($params['qy']==1){
                    if($row['level']==2 && $row['state']==1){
                        //B2申请提供服务 通过
//            if($data['choice']==0){
//                $where['order_id']=['eq',$find['order_id']];
//                $where['user_id']=['neq',$find['user_id']];
//                $edit=Twotg::where($where)->update(['tx'=>1]);
//                $order=BydShopOrder::where('id',$find['order_id'])->update(['twofw'=>$data['choice']]);
//            }
  $userxq=\app\admin\model\BydUser::find($row['user_id']);
                    $arr=[
                        'openid'=>$userxq['openid'],
                        'thing2'=>"您申请的提供服务审核已通过",//$data['news'],
                        'thing6'=>"请前往小程序查看",
                    ];
                    $new=new News();
                    $news=$new->vr($arr);
                        $order=\app\admin\model\BydShopOrder::where('id',$row['order_id'])->update(['twofw'=>1]);
                    }elseif($row['level']==3 && $row['state']==1){
                        $order=\app\admin\model\BydShopOrder::where('id',$row['order_id'])->update(['threefw'=>1]);
                    }elseif($row['state']==2){
                        //B2申请提供服务 拒绝
                        $userxq=\app\admin\model\BydUser::find($row['user_id']);
                        $arr=[
                            'openid'=>$userxq['openid'],
                            'thing2'=>"您申请的提供服务审核被驳回",//$data['news'],
                            'thing6'=>"请前往小程序查看",
                        ];
                        $new=new News();
                        $news=$new->vr($arr);
                        $where['order_id']=['eq',$row['order_id']];
                        $where['user_id']=['neq',$row['user_id']];
                        $findb=\app\admin\model\Twotg::where($where)->find();
                        if(empty($findb)){
                            //没有B3  订单直接排给平台
                            $add=new \app\admin\model\Twotg();
                            $add->data([
                                'user_id'=>1,
                                'order_id'=>$row['id'],
                                'level'=>3,
                                'endtime'=>'7962199200',
                                'tx'=>0,
                                'state'=>1
                            ]);
                            $add->save();

                        }else{
                            //B2拒绝后直接给B3
                            $userxq=\app\admin\model\BydUser::find($findb['user_id']);
                            $arr=[
                                'openid'=>$userxq['openid'],
                                'thing2'=>"您有一个订单需要您提供服务",//$data['news'],
                                'thing6'=>"请前往小程序查看",
                            ];
                            $new=new News();
                            $news=$new->vr($arr);
                            $add=new \app\admin\model\Twotg();
                            $add->data([
                                'user_id'=>$findb['id'],
                                'order_id'=>$row['order_id'],
                                'level'=>3,
                                'endtime'=>'7962199200',
                                'tx'=>0,
                                'state'=>1
                            ]);
                            $add->save();
                            $order=\app\admin\model\BydShopOrder::where('id',$row['order_id'])->update(['threefw'=>1]);
                        }
                    }
                }elseif($params['qy']==2){

                    if($row['level']==2 && $row['state']==1){

                        //B2提供服务被拒绝
                        $userxq=\app\admin\model\BydUser::find($row['user_id']);
                        $arr=[
                            'openid'=>$userxq['openid'],
                            'thing2'=>"您提供服务审核被拒绝",//$data['news'],
                            'thing6'=>"请前往小程序查看",
                        ];
                        $new=new News();
                        $news=$new->vr($arr);
                        $wheres['id']=['eq',$row['order_id']];
                       // $where['user_id']=['neq',$row['user_id']];
                        //$findb=\app\admin\model\Twotg::where($where)->find();
                        $findb=\app\admin\model\BydShopOrder::where($wheres)->find();

                        if(empty($findb['three'])){

                            $add=new \app\admin\model\Twotg();
                            $add->data([
                                'user_id'=>1,
                                'order_id'=>$row['id'],
                                'level'=>3,
                                'endtime'=>'7962199200',
                                'tx'=>0,
                                'state'=>1,
                                'qy'=>1
                            ]);
                            $add->save();
                        }else{

                            $userxq=\app\admin\model\BydUser::find($findb['three']);
                            $arr=[
                                'openid'=>$userxq['openid'],
                                'thing2'=>"您有一个订单需要您提供服务",//$data['news'],
                                'thing6'=>"请前往小程序查看",
                            ];
                            $new=new News();
                            $news=$new->vr($arr);

                            $add=new \app\admin\model\Twotg();
                            $add->data([
                                'user_id'=>$findb['three'],
                                'order_id'=>$row['id'],
                                'level'=>3,
                                'endtime'=>'7962199200',
                                'tx'=>0,
                                'state'=>1
                            ]);
                            $add->save();
                            $order=\app\admin\model\BydShopOrder::where('id',$row['order_id'])->update(['threefw'=>1]);
                            //$edit=\app\admin\model\Twotg::where($where)->update(['state'=>1]);
                        }
                       // $edit=\app\admin\model\Twotg::where($where)->update(['tx'=>1]);
                    }elseif($row['state']==2){
                        $userxq=\app\admin\model\BydUser::find($row['user_id']);
                        $arr=[
                            'openid'=>$userxq['openid'],
                            'thing2'=>"您拒绝的提供服务审核被驳回",//$data['news'],
                            'thing6'=>"请前往小程序查看",
                        ];
                        $new=new News();
                    }
                }
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    /**
     * 检测是否有新退货消息
     */
    public function isHaveNewAfter()
    {
        $cache=\think\Cache::get('fw');
        if(!empty($cache)){
            $tips = 'aftersale:tips:number';
            //edis = (new Redis())->getRedis();
            $tipsNumber = rand();
            $this->success('获取新培训费消息数量成功', null, $tipsNumber);
        }

    }

    /**
     * 已确认新退货消息
     */
    public function handleTui()
    {
        $type = $this->request->post('type');
        $tips = 'aftersale:tips:number';
        $tipsNumber = rand();
        \think\Cache::rm('fw');
        $this->success('确认培训费用消息成功', null, $type);
    }
}
